#!/usr/bin/perl
# commit.pl
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

use strict;
use warnings;
use lib '/home/manager/lib';
use CCNCore::Home;
use CCNCore::Scripting;
use CCNv2::Scripting::e164;

# This fixed the hangups at the end of the commit script.
open STDOUT, '>', CCNCore::Home::config.'/local/last_commit._{LOG}_.log' or die 'Cannot redirect STDOUT';
open STDERR, '>&', \*STDOUT or die 'Cannot dup STDOUT';

select STDOUT; $| = 1;
select STDERR; $| = 1;

our $force = 0;
our $debug = 1;
shift(@ARGV), $force = 1 if $#ARGV >= 0 and $ARGV[0] eq '-f';
shift(@ARGV), $debug = 1 if $#ARGV >= 0 and $ARGV[0] eq '-d';

our $ROOT = CCNCore::Home::config.'/local/content';

use CCNCore::Scripting::commit;
use CCN::Scripting::run_once;

my $c = new CCNCore::Scripting::commit($ROOT);
my $e = new CCNv2::Scripting::e164($ROOT);
my $r = new CCNCore::Scripting::run_once;

sub commit($$)      { $c->sub_if_changed(@_); }
sub commit_file($)  { $c->commit_file(@_); }
sub run_once($)     { $r->add(@_); }
sub run_them()      { $r->run(); }
sub e164_changed($$$) {
    my ($from,$to,$after) = @_;
    print STDERR "CCN-DEBUG: e164_process $from > $to => $after\n" if $debug;
    
    $e->run($from,$to);
    $c->commit_file($to);
    $r->add($after);
}

# This is used by the code inserted by COMMIT_MODULE().
# (Ideally all modules should be migrated to this scheme.)
use CCNCore::Provisioning::Local;
our $local = new CCNCore::Provisioning::Local;

# Instructions after this line are added by the post-processor.
# Leave the following blank line in the template!
